package com.tianzj.mongodb.controller;

import com.mongodb.client.result.UpdateResult;
import com.tianzj.mongodb.entity.User;
import com.tianzj.mongodb.serivice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Auther: admin
 * @Date: 2019/7/22 15:26
 * @Description:
 */
@RestController
@RequestMapping("/mongodb")
public class mongodbController {
    @Autowired
    private UserService userService;

    @RequestMapping("/")
    public String index(){
        String name ="tianzj";
        User user = userService.findByName(name);
        if(user!=null){
            return user.getName();
        }
        return "没查出来";
    }
    @RequestMapping("/find")
    public String find(){
        List<User> users = userService.findAll();
        if (users!=null){
            return users.toString();
        }
        return "没查出来";
    }
    @RequestMapping("/save")
    public String save(){
        User user = new User();
        user.setName("张猛");
        user.setId("122");
        user.setAge(23);
        user.setSex("男");
        User user1=userService.save(user);
        return user1.toString();
    }

    @RequestMapping("/update")
    public String update(){
        UpdateResult update = userService.update();
        return update.toString();
    }
}
